<script type="text/javascript">
    var GLOBAL = {
        time : <?php echo "$time,"; ?>
    };
    function validaLDAP(username){
        if(document.getElementById("Usuarios_USRSEDES").value == ""){
            alert("Debe elegir primero la sede antes de ingresar el usuario");
            username.value = "";
            return false;
        }
        $.post("<?php echo $this->createUrl("usuarios/validaLDAP"); ?>",{user:username.value,sede:document.getElementById("Usuarios_USRSEDES").value},function(data){
            switch(parseInt(data)){
                case 1:
                    alert("Sin conexion");
                    return false;
                    break;
                case 4:
                    alert("Usuario incorrecto");
                    username.value = "";
                    return false;
                    break;
                case 5:
                    alert("Imposible conexión");
                    username.value = "";
                    return false;
                    break;
                case 6:
                    alert("Protocolo invalido");
                    username.value = "";
                    return false;
                    break;
            }
        }); //fin $.pos("<?php ?>",{},function(data){
        //        console.log(document.getElementById("Usuarios_USRSEDES").value);
    } //fin function validaLDAP(username){
    function agregaRol(){
        var roles = document.getElementById("selRoles").value;
        if(roles == ""){
            return false;
        }
        $("#divTabla").load("<?php echo $this->createUrl("usuarios/agregaRol"); ?>",{time:GLOBAL.time,rol:roles},function(data){
            
        }); //fin $("#divTabla").load("<?php ?>",{},function(data){
    } //fin function agregaRol(){
    function deleteRol(id){
        if(id == "") return false;
        $("#divTabla").load("<?php echo $this->createUrl("usuarios/eliminaRol"); ?>",{time:GLOBAL.time,tmpId:id},function(data){
            
        }); //fin $("#divTabla").load("<?php ?>",{},function(data){
    } //fin function deleteRol(id){
</script>
<style type="text/css">
    .divAccion{
        clear: both;
        float: left;
        width: 20%;
        text-align: center;
    }
    .divRol{
        float: left;
        width: 80%;
        /*        text-align: center;*/
    }
    .clTitRol{
        width: 100%;
    }
    .divContieneRol{
        width: 100%;
        margin: 1%;
    }
    .divContieneRol:hover{
        background-color: #a6c3cc;
        color: #B2919A;
    }
    .divTitAccion{
        width: 20%;
        font-weight: bolder;
        float: left;
        padding: 2%;
        background-color: #B2919A;
        color: #ffe9ef;
    }
    .divTitRol{
        width: 72%;
        text-align: center;
        font-weight: bolder;
        float: left;
        padding: 2%;
        background-color: #B2919A;
        color: #ffe9ef;
    }
    #divTabla{
        width: 100%;

    }
</style>
<div class="form">
    <?php
    $connection = Yii::app()->db;
    $form = $this->beginWidget('GxActiveForm', array(
        'id' => 'usuarios-form',
        'enableAjaxValidation' => false,
            ));
    ?>
    <p class="note">
        <?php echo Yii::t('app', 'Campos con '); ?> <span class="required">*</span> <?php echo Yii::t('app', ' son requeridos'); ?>.
    </p>
    <?php echo $form->errorSummary($model); ?>
    <div class="row">
        <?php echo $form->labelEx($model, 'USRSEDES'); ?>
        <?php echo $form->dropDownList($model, 'USRSEDES', array('' => 'Seleccione') + GxHtml::listDataEx(Sedes::model()->findAll(), 'SEDECODIGO', 'SEDENOMBRE'), array('class' => 'Ccombo')); ?>
        <?php echo $form->error($model, 'USRSEDES'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'USRNOMBRE'); ?>
        <?php echo $form->textField($model, 'USRNOMBRE', array('maxlength' => 45)); ?>
        <?php echo $form->error($model, 'USRNOMBRE'); ?>
    </div><!-- row -->
    <div class="row">
        <?php echo $form->labelEx($model, 'USRUSUARIO'); ?>
        <?php echo $form->textField($model, 'USRUSUARIO', array('maxlength' => 45, 'onchange' => 'validaLDAP(this);')); ?>
        <?php echo $form->error($model, 'USRUSUARIO'); ?>
    </div><!-- row -->
    <div class="row">
        <label for="selRoles">Roles</label>
        <?php
        $command = $connection->createCommand("select RLSID,RLSNOMBRE FROM ROLES ORDER BY RLSNOMBRE");
        $results = $command->queryAll();
        $campo = "<select id='selRoles' class='Ccombo' >";
        $campo .= "<option value=''>Seleccione...</option>";
        foreach ($results as $rows) {
            $valor = array_values($rows);
            $campo .="<option value='$valor[0]'>$valor[1]</option>";
        }
        $campo .= "</select>";
        echo $campo;
        ?>
        <button type="button" onclick="agregaRol();" class="buttonA">Añadir rol</button>
    </div>
    <div style="width: 60%">
        <div class="clTitRol">
            <div class="divTitAccion" >ACCIÓN</div>
            <div class="divTitRol" >ROL</div>
        </div>
        <div id="divTabla">
            <?php
            $con = Yii::app()->db;
            $sql = "select TMP.TEMPORALID,ROL.RLSNOMBRE FROM TEMPORAL TMP
                INNER JOIN ROLES ROL ON ROL.RLSID = TMP.TEMPORALINT1
                WHERE TMP.TEMPORALINDICE = $time";
            $result = $con->createCommand($sql);
            $result = $result->queryAll();
            $campos = "";
            if (count($result) > 0)
                foreach ($result as $row) {
                    $rows = array_values($row);
                    $campos .= "<div class='divContieneRol'><div class='divAccion'><a href='javascript:deleteRol({$rows[0]});' title='Eliminar rol {$rows[1]}'>Eliminar</a></div>";
                    $campos .= "<div class='divRol'>{$rows[1]}</div></div>";
                }
            echo $campos;
            ?>
        </div> <!-- fin divTabla -->
    </div>
    <div style="clear: both;"></div>
    <?php
    echo "<input type='hidden' name='time' value='$time' />";
    echo GxHtml::submitButton(Yii::t('app', 'Guardar Usuario'));
    $this->endWidget();
    ?>

</div><!-- form -->